define(['app'],function(app){
    app.controller('CountCtrl' ,['$scope','$rootScope', '$timeout', '$http','$q','countService',
       function ($scope, $rootScope, $timeout,$http, $q,countService) {  
        
        //代码书写步骤
        //1.用【$scope】定义公共类model,和页面绑定的model
        //2.用【var】定义controller内部用的变量
        //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
        //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
        //定义规则，按可用性分忧优先级，公共的在前，private的在后

        //bookCount 阅读笔记统计
        $scope.histogramXAxisData = [];//类型
        $scope.histogramSeriesData = [];//数据
         //图书借阅统计
        $scope.pieBorrowSeriesData = [];//数据
         //借阅人借阅次数统计
        $scope.pieBorrowNumSeriesData = [];//数据
        //本年度阅读笔记统计
        $scope.lineReadingNotesData = {
            legendData:[],
            seriesData: []
        };

        //生成阅读笔记统计 数据格式
        var createHistogramCountData = function(data,xAxisData,seriesData){
            for(var i=0;i< data.length;i++){
                var val = data[i];
                xAxisData.push(val.name);
                seriesData.push(val.count);
            }
        }

        //生成饼图 数据格式
        var createPieCountData = function(data,seriesData){
            for(var i=0;i< data.length;i++){
                var val = data[i];
                var tempModel = {
                    name:data[i].name,
                    value:data[i].count
                };
                seriesData.push(tempModel);
            }
        }

        //生成折线图
        var createLineCountData = function(data,lineData){
            for(var i=0;i<data.length;i++){
                lineData.legendData.push(data[i].cate);
                var seriesModel = {
                    name:data[i].cate,
                    type:'line',
                    stack: '总量',
                    areaStyle: {normal: {}},
                    data:data[i].count
                };
               lineData.seriesData.push(seriesModel);
            }
        }

        $scope.countData = [];
        $scope.retrCountList = function(){
            countService.retrCountList().then(
                function(response){
                    $scope.countData = response;
                    //生成对应的图形统计数据
                    createHistogramCountData($scope.countData.bookCount,$scope.histogramXAxisData,$scope.histogramSeriesData);  
                    createPieCountData($scope.countData.borrowInfoCount,$scope.pieBorrowSeriesData);
                    createPieCountData($scope.countData.borrowerCount,$scope.pieBorrowNumSeriesData);
                    createLineCountData($scope.countData.bookNoteCount,$scope.lineReadingNotesData);
                },
                function(code){
                    alert("系统出现问题了！");
                }
            );
        }

        //柱状图
        $scope.histogram = {
            color: ['#3398DB'],
            tooltip: {
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            grid: {
                left: '5%',
                right: '5%',
                bottom: '5%',
                top:'5%',
                containLabel: true
            },
            xAxis: [{
                type: 'category',
                // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
                data: $scope.histogramXAxisData,
                axisTick: {
                    alignWithLabel: true
                }
            }],
            yAxis: [{
                type: 'value'
            }],
            series: [{
                name: '直接访问',
                type: 'bar',
                barWidth: '60%',
                // data: [10, 52, 200, 334, 390, 330, 220]
                data: $scope.histogramSeriesData
            }]
        };
                   
        //饼图1 图书借阅统计
        $scope.pieBorrowCount = {
            tooltip : {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            toolbox: {
                show : true,
                feature : {
                    mark : {show: true},
                    dataView : {show: true, readOnly: false},
                    magicType : {
                        show: true,
                        type: ['pie', 'funnel']
                    },
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
            calculable : true,
            series : [
                {
                    name:'面积模式',
                    type:'pie',
                    radius : [30, 110],
                    center : ['50%', '50%'],
                    roseType : 'area',
                    data: $scope.pieBorrowSeriesData
                }
            ]
        };

        //饼图2 借阅人借阅次数统计
        $scope.pieBorrowNum =  {
            title : {
            },
            tooltip : {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            series : [
                {
                    name: '详细数据',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '50%'],
                    data: $scope.pieBorrowNumSeriesData,
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };

        //堆叠区域显示图 本年度阅读笔记统计
        $scope.lineReadingNotes = {
            tooltip : {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            legend: {
                // data:['文学','科技','历史','心里','传记']
                data:$scope.lineReadingNotesData.legendData
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis : [
                {
                    type : 'category',
                    boundaryGap : false,
                    data : ['一月','二月','三月','四月','五月','六月',
                    '七月','八月','九月','十月','十一月','十二月']
                }
            ],
            yAxis : [
                {
                    type: 'value'
                }
            ],
            series: $scope.lineReadingNotesData.seriesData
        };

        var init = function () {
            $scope.dashboardModel.isActiveMenu = 'count';//左侧菜单按钮赋初值
            $scope.retrCountList();
        };

        init();
    }]);
});